package cn.tedu.logistics.interceptor;


import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Component
public class UserInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("拦截器正在运行");
    	Cookie[] cookies = request.getCookies();
        String User =null;
        if(cookies!=null&&cookies.length>0){
            for (Cookie c:cookies) {
                if("UserCookie".equals(c.getName())){
                    User=c.getValue();
                    break;
                }
            }
        }
        if (!StringUtils.isEmpty(User)){
            return true;
        }
        response.sendRedirect("/showIndex.do");
        return false;
    }
}
